import pygame, sys

pygame.init()
screen = pygame.display.set_mode ([400,600])
screen.fill ([0,0,0])
ship = pygame.image.load('lunarlander.png')
moon = pygame.image.load('moonsurface.png')
ground = 540
start = 90
clock = pygame.time.Clock()
ship_mass = 5000.0
fuel = 5000.0
velocity = -100.0
gravity = 10
hieght = 2000
thrust = 0
delta_V = 0
y_pos = 90
held_down = False
class ThrottleClass (pygame.sprite.Sprite):
    def __init__(self,location = [0,0]):
        pygame.sprite.Sprite.__init__(self)
        image_surface = pygame.surface.Surface([30,10])
        image_surface.fill([128,128,128])
        self.image = image_surface.convert()
        self.rect = self.image.get_rect()
        self.rect.left , self.rect.centery = location

def calculate_velocity():
    global thrust, fuel, velocity, delta_v, height , y_pos
    delta_t =  1/fps
    thrust =  (500 - myThrottle.rect.centery) * 5
    fuel -= thrust /(10* fps)
    if fuel < 0 :  fuel = 0.0
    if fuel < 0.1 :  thrust = 0.0
    delta_v = delta_t *(-gravity + 200*thrust/(ship_mass+fuel))
    velocity = velocity +delta_v
    delta_h = velocity * delta_t
    height = height + delta_h
    y_pos = ground - (hieght*(ground - start)/ 2000)-90
    
def display_stats():
    v_str = "velocity: %i m/s"% velocity
    h_str = "height: %.1f "% height
    t_str = "thrust: %i "% thrust
    a_str = "acceleration: %.1f "% acceleration
    f_str = "fuel: %i "% fuel
    v_font = pygame.font.Font(None, 26)
    v_surf =  v_font.render(v_str, 1,(255,255,255))
    screen.blit(v_surf, [10,50])
    a_font = pygame.font.Font(None, 26)
    a_surf =  v_font.render(a_str, 1,(255,255,255))
    screen.blit(a_surf, [10,50])
    h_font = pygame.font.Font(None, 26)
    h_surf =  h_font.render(h_str, 1,(255,255,255))
    screen.blit(h_surf, [10,50])
    t_font = pygame.font.Font(None, 26)
    t_surf =  t_font.render(t_str, 1,(255,255,255))
    screen.blit(t_surf, [10,50])
    f_font = pygame.font.Font(None, 26)
    f_surf =  f_font.render(f_str, 1,(255,255,255))
    screen.blit(f_surf, [10,50])
    
def  display_flames():
    flame_size = thrust / 15
    for i in range (2):
        startx = 252-10+i *19
        starty = y_pos + 83
        pygame.draw.polygon(screen,[255,109,14],[(startx,starty)
                                   (startx + 4, starty+flame_size)
                                   (startx + 8, starty)], 0)
def display_final():
    final1 = "Gameover"
    if velocity > -5:
        final2 = "Nice landing"
        final3 = "I hear NASA is hiring"
    elif velocity > -15:
        final2 = "thats rough!!"
        final3 = "but you survived"
    else:
        final2 = "your dead"
        final3 = "AH!!!!!!!!!!!!!!!!!!!!!"
    pygame.dra
    f1_font = pygame.font.Font(None, 70)
    f1_surf =  f1_font.render(final1, 1,(255,255,255))
    screen.blit(f1_surf, [20,50])
    f1_font = pygame.font.Font(None, 70)
    f2_surf =  f2_font.render(final2, 1,(255,255,255))
    screen.blit(f2_surf, [20,110])
    f3_font = pygame.font.Font(None, 70)
    f3_surf =  f3_font.render(final3, 1,(255,255,255))
    screen.blit(f3_surf, [20,150])
    pygame.display.flip()
    
myThrottle = ThrottleClass([15,500])

while True :
    clock.tick(30)
    fps = clock.get_fps()
    if fps < 1: fps = 30
    if hieght > 0.01:
        calculate_velocity()
        screen.fill([0,0,0])
        display_stats()
        pyagame.draw.rect(screen,[0,0,254],[80,350,24,100])
        fuelbar = 96*fuel/5
        pyagame.draw.rect(screen,[0,255,0],
                         [84,448-fuelbar,18,fuelbar],0)
        pyagame.draw.rect(screen,[255,0,0],
                         [25,300,10,200],0)
        screen.blit(moon,[0,500,400,100])
        pyagame.draw.rect(screen,[60,60,60],
                         [220,535,70,5],0)
        screen.blit(myThrottle.image,myThrottle.rect)
        display_flames()
        screen.blit(ship,[230,ypos,50,90])
        instruct1 = "Land softly without running out of gas"
        instruct2 = "Good landing"
        inst1_font = pygame.font.Font(None,24)
        inst1_surf = inst1_font,render(indstruct1,1,(255,255,255))
        screen.blit(inst1_surf[50,550])
        inst2_font = pygame.font.Font(None,24)
        inst2_surf = inst2_font,render(indstruct2,1,(255,255,255))
        screen.blit(inst2_surf[20,575])
        pygame.display.flip()
        
    else:
        display_final()
        
    for event in pygmae.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            held_down= True
        elif event.type == pygame.MOUSEBUTTONUP:
            held_down= False 
        elif event.type == pygame.MOUSEBUTTONMOTION:
            if held_down:
                myThrottle.rect.centery = event.pos[1]
                if myThrottle.rect.centery < 300:
                    myThrottle.rect.centery = 300
                if myThrottle.rect.centery < 500:
                    myThrottle.rect.centery = 500                    